package com.yanghongyue.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.databind.util.BeanUtil;
import com.yanghongyue.reggie.bean.Category;
import com.yanghongyue.reggie.bean.Dish;
import com.yanghongyue.reggie.bean.Flavor;
import com.yanghongyue.reggie.bean.Setmeal;
import com.yanghongyue.reggie.bean.dto.DishDto;
import com.yanghongyue.reggie.common.R;
import com.yanghongyue.reggie.service.CategoryService;
import com.yanghongyue.reggie.service.DishService;
import com.yanghongyue.reggie.service.FlavorService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author HongyueYang
 */
@RestController
@RequestMapping("/dish")
public class DishController {

    @Autowired
    private DishService dishService;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private FlavorService flavorService;

    //分页关联查询
    @RequestMapping("/page")
    public R<Page> page(int page, int pageSize, String name) {

        //创建条件构造器
        QueryWrapper<Dish> wrapper = new QueryWrapper<>();
        //设置排序
        wrapper.orderByDesc("sort");
        //模糊查询
        if (name != null && !name.equals("")) {
            wrapper.like("name", name);
        }

        //创建分页对象
        Page<Dish> pageInfo = new Page<>(page, pageSize);
        //执行分页查询
        dishService.page(pageInfo, wrapper);


        //创建菜品dto分页对
        Page<DishDto> dtoPageInfo = new Page<>();
        //复制分页属性
        BeanUtils.copyProperties(pageInfo,dtoPageInfo,"records");
        //获取菜品分页对象的菜品列表
        List<Dish> dishList = pageInfo.getRecords();
        //创建菜品dto列表
        List<DishDto> dtoList = new ArrayList<>();
        //遍历菜品集合
        for (Dish dish : dishList) {
            //创建dto对象
            DishDto dto = new DishDto();
            //复制属性
            BeanUtils.copyProperties(dish,dto);
            //根据分类id查询分类对象
            Category category = categoryService.getById(dish.getCategoryId());
            //如果当前分类不为空
            if(category!=null){
                //给dto对象设置分类名称
                dto.setCategoryName(category.getName());
            }
            //将dto对象添加到dto列表中
            dtoList.add(dto);
        }
        //给dto的分页对象重新设置集合
        dtoPageInfo.setRecords(dtoList);
        //返回结果
        return R.sucess(dtoPageInfo);

    }


    //添加菜品
    @PostMapping
    public R<String> save(@RequestBody DishDto dto){
        //调用业务层方法
        dishService.saveDish(dto);
        return R.sucess("添加菜品成功");
    }

    //根据id查询菜品及其对应的口味信息
    @GetMapping("/{id}")
    public R<DishDto> getDishWithFlavors(@PathVariable("id") Long id){
        return R.sucess(dishService.getDishWithFlavors(id));
    }
    //修改菜品
    @PutMapping
    public R<String> updateDishWithFlavors(@RequestBody DishDto dto){
        dishService.updateDishWithFlavors(dto);
        return R.sucess("修改菜品成功");
    }

    //修改状态
    @PostMapping("/status/{status}")
    public R<String> updateStatus(@PathVariable("status") Integer status,Long[] ids){
        for (Long id : ids) {
            Dish dish = new Dish();
            dish.setId(id);
            dish.setStatus(status);
            dishService.updateById(dish);
        }
        return R.sucess("修改状态成功");
    }

    //删除菜品
    @DeleteMapping
    public R<String> delete(Long[] ids){
        return dishService.removeByIds(Arrays.asList(ids))?R.sucess("删除成功"):R.error("删除失败");
    }

    //根据分类id查询菜品列表
    @GetMapping("/list")
    public R<List> list(Long categoryId) {
        //创建条件构造器
        QueryWrapper<Dish> wrapper = new QueryWrapper<>();
        wrapper.eq("category_id", categoryId);
        wrapper.eq("status", 1);
        wrapper.orderByDesc("sort");
        //查询
        List<Dish> dishList = dishService.list(wrapper);

        //创建dto集合
        List<DishDto> dtoList = new ArrayList<>();
        //遍历菜品集合
        for (Dish dish : dishList) {
            //创建dto对象
            DishDto dto = new DishDto();
            //复制属性
            BeanUtils.copyProperties(dish,dto);
            //通过菜品id查询口味列表
            QueryWrapper<Flavor> flavorQueryWrapper = new QueryWrapper<>();
            flavorQueryWrapper.eq("dish_id",dish.getId());
            List<Flavor> flavorList = flavorService.list(flavorQueryWrapper);
            //将口味集合设置给dto对象
            dto.setFlavors(flavorList);
            //将dto对象添加到dto集合中
            dtoList.add(dto);
        }
        return R.sucess(dtoList);
    }

}
